//
//  MyViewController.m
//  DSYDouban
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "MyViewController.h"
#import "UserLoginViewController.h"
#import "DBManager.h"

@interface MyViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSDictionary *user;

@end

@implementation MyViewController

- (void)dealloc
{
    [_tableView release];
    
    
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"用户信息";
    
    NSMutableString *title;
    if ([self hasCurrentUser] == YES) {
        title = [NSMutableString stringWithFormat:@"注销"];
    } else {
        title = [NSMutableString stringWithFormat:@"登录"];
    }
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction:)];
    self.navigationItem.rightBarButtonItem = rightButton;
    [rightButton release];
    
    
    [self createTableView];
    
    
}




- (void)rightButtonAction: (UIBarButtonItem *)buttonItem {
    if ([self hasCurrentUser] == NO) {
        buttonItem.title = @"注销";
        
        UserLoginViewController *userloginVC = [[UserLoginViewController alloc] init];
        
//        userloginVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
//        [self presentViewController:userloginVC animated:YES completion:^{
//            
//        }];
        [self.navigationController pushViewController:userloginVC animated:YES];
        
        [userloginVC release];
        
    } else {
        buttonItem.title = @"登陆";
        
        
        [self setStatus:0 withUserName:[_user objectForKey:@"username"]];
        
        NSLog(@"确定注销吗?");
    }
    
}

- (void)setStatus:(NSInteger)status withUserName:(NSString *)username {
    [[DBManager createDB] update:[NSString stringWithFormat:@"update userInfo set status = %ld where username = '%@'", status, username]];
}


// 查看当前登录的用户
- (BOOL) hasCurrentUser {
    
    self.user = [[DBManager createDB] currentUser];
    
    if ([_user objectForKey:@"username"] != NULL) {
        return YES;
    } else {
        return NO;
    }
    
}

// 创建tableView
- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    if (indexPath.row == 0) {
        cell.textLabel.text = @"我的活动";
    }
    if (indexPath.row == 1) {
        cell.textLabel.text = @"我的电影";
    }
    if (indexPath.row == 2) {
        cell.textLabel.text = @"清除缓存";
    }
    
    cell.textLabel.font = [UIFont systemFontOfSize:23];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return (_tableView.frame.size.height - 64) / 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld", indexPath.row);
    if (indexPath.row == 2) {
        // 删除活动收藏表的所有数据
        NSString *sql = [NSString stringWithFormat:@"delete from activityCollection where username = '%@'", [_user objectForKey:@"username"]];
        
        NSLog(@"%@", [_user objectForKey:@"username"]);
        
        [[DBManager createDB] deleteData:sql];
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"消息提示" message:@"清除了所有的缓存" preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alert animated:YES completion:^{
            [NSThread sleepForTimeInterval:1.5];
            [alert dismissViewControllerAnimated:YES completion:^{
                
            }];
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
